package com.kaiwukj.android.ufamily.mvp.ui.page.mine.address;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerMineComponent;
import com.kaiwukj.android.ufamily.di.module.MineModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyAddressResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.mine.MineContract;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.MyAddressUrl)
public class MyAddressActivity extends BaseMvpActivity<MyAddressPresenter> implements MineContract.AddressView {

    private static final int UPDATE = 102;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;

    private MyAddressAdapter mListAdapter;

    private final int page = 1;
    private final int pageSize = 25;

    private int delPos = -1;

    @Autowired
    boolean isPick = false;


    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @OnClick(R.id.btn_add)
    void intent2Add() {
        ARouter.getInstance().build(ARouterUrlKt.MyAddressEditUrl)
                .navigation(this, UPDATE);
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerMineComponent.builder()
                .appComponent(appComponent)
                .mineModule(new MineModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_address_my;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("我的地址");
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.fetchList(page, pageSize);
        });
        refreshLayout.setEnableLoadMore(false);
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.addItemDecoration(new SpaceItemDecoration(1));
        mListAdapter = new MyAddressAdapter();
        mListAdapter.setOnItemChildClickListener(new MyAddressAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int pos) {
                switch (view.getId()) {
                    case R.id.tv_delete:
                        showMessageOKCancel(MyAddressActivity.this, "是否删除", v -> {
                            delPos = pos;
                            mPresenter.deleteAddress((int) mListAdapter.getItemId(pos));
                        });
                        break;
                    case R.id.container_edit:
                        ARouter.getInstance().build(ARouterUrlKt.MyAddressEditUrl)
                                .withSerializable("addressResult", mListAdapter.getItem(pos))
                                .navigation(MyAddressActivity.this, UPDATE);
                        break;
                }
            }

            @Override
            public void onItemClick(View view, int pos) {
                if (isPick) {
                    Intent intent = new Intent();
                    intent.putExtra("addr", mListAdapter.getItem(pos));
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });

        rvList.setAdapter(mListAdapter);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        emptyView.show(true);
        mPresenter.fetchList(page, pageSize);
    }

    @Override
    public void onFetchAddress(List<MyAddressResult> results) {
        emptyView.hide();
        mListAdapter.setList(results);
        refreshLayout.finishRefresh(true);
        if (mListAdapter.getItemCount() == 0) {
            emptyView.show("暂未添加地址", "");
        }
    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (taskId == MyAddressPresenter.DELETE) {
            showMessage("操作成功");
            hideLoading();
            if (delPos != -1) {
                mListAdapter.removeAt(delPos);
                delPos = -1;
            }
        }
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        refreshLayout.finishRefresh();
        hideLoading();
        emptyView.hide();
        if (taskId == MyAddressPresenter.DELETE) {
            delPos = -1;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == UPDATE) {
            if (RESULT_OK == resultCode) {
                mPresenter.fetchList(page, pageSize);
            }
        }
    }
}
